

package com.hazelcast.jet.impl.connector;

import com.hazelcast.jet.SimpleTestInClusterSupport;
import com.hazelcast.test.jdbc.JdbcDatabaseProvider;

import javax.sql.CommonDataSource;

/**
 * Test support class that contains TestDatabaseProvider
 */
public abstract class JdbcDatabaseProviderTestSupport extends SimpleTestInClusterSupport {

    private static JdbcDatabaseProvider jdbcDatabaseProvider;

    public static JdbcDatabaseProvider getJdbcDatabaseProvider() {
        return jdbcDatabaseProvider;
    }

    public static void setJdbcDatabaseProvider(JdbcDatabaseProvider dbProvider) {
        jdbcDatabaseProvider = dbProvider;
        jdbcDatabaseProvider.createDatabase("test");
    }

    protected static String getJdbcUrl() {
        return jdbcDatabaseProvider.url();
    }

    protected static String getUsername() {
        return jdbcDatabaseProvider.user();
    }

    protected static String getPassword() {
        return jdbcDatabaseProvider.password();
    }

    protected static String getDatabaseName() {
        return jdbcDatabaseProvider.getDatabaseName();
    }

    protected static CommonDataSource createDataSource(boolean xa) {
        return jdbcDatabaseProvider.createDataSource(xa);
    }

    public static void shutdownDatabaseProvider() {
        if (jdbcDatabaseProvider != null) {
            jdbcDatabaseProvider.shutdown();
            jdbcDatabaseProvider = null;
        }
    }
}
